<template>
  <div>
    <el-table style="width: 100%; margin-top: 20px" border :data="records">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="skuName" label="名称" width="width" />
      <el-table-column prop="skuDesc" label="描述" width="width" />
      <el-table-column prop="prop" label="默认图片" width="110">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" alt="" style="width: 80px; height: 80px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="80" align="center" />
      <el-table-column prop="price" label="价格(元)" width="80" align="center" />
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <!-- 上架 -->
          <el-button
            v-if="row.isSale == 0"
            type="success"
            icon="el-icon-bottom"
            size="mini"
            @click="onSale(row)"
          />
          <!-- 下架 -->
          <el-button
            v-else
            type="info"
            icon="el-icon-top"
            size="mini"
            @click="cancelSale(row)"
          />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit" />
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          />
          <el-button type="danger" icon="el-icon-delete" size="mini" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[5, 10, 15]"
      :page-size="limit"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
    />

    <!-- 抽屉 -->
    <el-drawer :visible.sync="show" :show-close="false" size="50%" :with-header="true">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              v-for="attr in skuInfo.skuAttrValueList"
              :key="attr.id"
              type="success"
              style="margin-right: 10px"
            >{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      records: [],
      skuInfo: {},
      show: false
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      const result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    // 上架
    async onSale(row) {
      const result = await this.$API.sku.reqOnSale(row.id)
      if (result.code == 200) {
        row.isSale = 1
        this.$message({
          type: 'success',
          message: '上架成功！'
        })
      }
    },
    // 下架
    async cancelSale(row) {
      const result = await this.$API.sku.reqCancelSale(row.id)
      if (result.code == 200) {
        row.isSale = 0
        this.$message({
          type: 'success',
          message: '下架成功！'
        })
      }
    },
    // 编辑.....开发中
    edit() {
      this.$message({
        type: 'info',
        message: '开发中...'
      })
    },
    // 获取sku详情
    async getSkuInfo(sku) {
      // 获取sku数据
      this.show = true
      const result = await this.$API.sku.reqSkuById(sku.id)
      if (result.code == 200) {
        this.skuInfo = result.data
      }
    }
  }
}
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
</style>

<style>
.el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}
</style>
